package com.shy.springframework.beans.factory.support;

import cn.hutool.core.bean.BeanUtil;
import com.shy.springframework.beans.BeansException;
import com.shy.springframework.beans.PropertyValue;
import com.shy.springframework.beans.PropertyValues;
import com.shy.springframework.beans.factory.config.BeanDefinition;
import com.shy.springframework.beans.factory.config.BeanReference;

import java.lang.reflect.Constructor;

/**
 * @author yzpang
 * @desc: 根据Bean定义实例化Bean
 * @date: 2023/2/7 14:36
 */
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory{
    private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();

    public InstantiationStrategy getInstantiationStrategy() {
        return instantiationStrategy;
    }

    @Override
    protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException {
        Object bean = null;
        try {
            // 1.实例化bean
            bean = createBeanInstance(beanDefinition, beanName, args);
            // 2.bean填充属性
            applyPropertyValues(beanName, bean, beanDefinition);
        } catch (Exception ex){
            throw new BeansException("Instantiation of bean failed.", ex);
        }
        // 3.注册单例bean
        addSingleton(beanName, bean);
        return bean;
    }

    /**
     * 创建Bean实例
     * @param beanDefinition
     * @param beanName
     * @param args
     * @return
     */
    protected Object createBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args)  {
        Constructor constructorToUse = null;
        Class<?> beanClass = beanDefinition.getBeanClass();
        Constructor<?>[] declaredConstructors = beanClass.getDeclaredConstructors();
        for (Constructor<?> ctor : declaredConstructors) {
            if (args != null && ctor.getParameterTypes().length == args.length){
                constructorToUse = ctor;
                break;
            }
        }
        return getInstantiationStrategy().instantiate(beanDefinition, beanName, constructorToUse, args);
    }

    /**
     * bean属性注入
     * @param beanName
     * @param bean
     * @param beanDefinition
     */
    protected void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition){
        try{
            PropertyValues propertyValues = beanDefinition.getPropertyValues();
            for (PropertyValue propertyValue : propertyValues.getPropertyValues()) {
                String name = propertyValue.getName();
                Object value = propertyValue.getValue();
                if (value instanceof BeanReference){
                    BeanReference beanReference = (BeanReference) value;
                    value = getBean(beanReference.getBeanName());
                }
                BeanUtil.setFieldValue(bean, name, value);
            }
        }catch (Exception ex){
            throw new BeansException("Error setting property values: " + beanName);
        }
    }

}
